package cn.zwk.entity;

import cn.zwk.resolver.MetricListText;
import lombok.Data;

import java.util.List;

@Data
public class Snapshoot {
    private List<MetricList> metricList;
    private long timestamp;
    private long interval_ms;
    private String hostName;

    public static Snapshoot build(List<MetricListText> metricsListText) {
        return build(metricsListText, System.currentTimeMillis());
    }

    public static Snapshoot build(List<MetricListText> metricsListText, long timestamp) {
        return build(metricsListText, timestamp, null);
    }
    
    public static Snapshoot build(List<MetricListText> metricsListText, long timestamp, String hostName) {
        return build(metricsListText, timestamp, hostName, 0);
    }
    
    public static Snapshoot build(List<MetricListText> metricsListText, long timestamp, String hostName, long intervalMs) {
        Snapshoot snapshoot = new Snapshoot();
        snapshoot.setTimestamp(timestamp);
        snapshoot.setHostName(hostName);
        snapshoot.setInterval_ms(intervalMs);

        if (metricsListText == null || metricsListText.isEmpty()) {
            return snapshoot;
        }

        List<MetricList> metricLists = metricsListText.stream()
                .map(metricListText -> metricListText.getHandler().buildMetric(metricListText))
                .filter(metricList -> metricList != null)
                .toList();

        snapshoot.setMetricList(metricLists);
        return snapshoot;
    }
    
    // 添加便捷方法来设置双向关联
    public void setMetricList(List<MetricList> metricList) {
        this.metricList = metricList;
        if (metricList != null) {
            for (MetricList list : metricList) {
                list.setParent(this);
            }
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (metricList != null) {
            for (MetricList list : metricList) {
                sb.append(list.toString());
            }
        }
        return sb.toString();
    }
}
